/**
 * v-p 操作权限处理指令
 */

import type { DirectiveBinding } from "vue";
import { usePermissionStore } from "../stores";

const hasPermiDirective: any = {
  mounted(el: HTMLElement, binding: DirectiveBinding) {
    const { value } = binding;
    const allPermission = "*:*:*";
    const permissions = usePermissionStore().permissions;

    if (value && Array.isArray(value) && value.length > 0) {
      const permissionFlags = value;

      const hasPermissions = permissions.some((permission) => {
        return allPermission === permission || permissionFlags.includes(permission);
      });

      if (!hasPermissions) {
        el.parentNode?.removeChild(el);
      }
    } else {
      console.error("请设置操作权限标签值");
    }
  }
};

export default hasPermiDirective;
